package org.september.pisces.user.permission.service;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;

import org.september.simpleweb.auth.DefaultMethod;
import org.september.simpleweb.auth.PublicMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Service
public class LoadPermissionService implements ApplicationListener<ContextRefreshedEvent>{
	
	@Autowired
	private PiscesPermissionService piscesPermissionService;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getParent() == null) {
            // 父容器为空，说明到达根容器，bean加载完成
            System.out.println("所有的bean都加载完成了");
            
            RequestMappingHandlerMapping mapping = event.getApplicationContext().getBean(RequestMappingHandlerMapping.class);
            Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
            
            for (Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {

                RequestMappingInfo requestMappingInfo = entry.getKey();
                PathPatternsRequestCondition cond = requestMappingInfo.getPathPatternsCondition();
                
                if(cond==null) {
                	System.out.println();
                	continue;
//                	cond.getPatterns().iterator().next();
                }
                String url = cond.getPatterns().iterator().next().getPatternString();
                HandlerMethod mappingInfoValue = entry.getValue();
                Method method = mappingInfoValue.getMethod();
                
                addDefaultUrl(method,url);
                addPublicUrl(method,url);
            
                System.out.println(url);
//                PiscesPermission permAnno = method.getAnnotation(PiscesPermission.class);
//                if(permAnno!=null) {
//                	piscesPermissionService.addPermissionData(permAnno.path(), url);
//                }
            }
        }
	}
	
	private void addDefaultUrl(Method method,String url) {
		DefaultMethod defualtAnno = method.getDeclaringClass().getAnnotation(DefaultMethod.class);
        if (defualtAnno == null) {
            defualtAnno = method.getAnnotation(DefaultMethod.class);
        }
        if (defualtAnno != null) {
        	piscesPermissionService.addDefaultUrl(url);
        }
	}
	
	private void addPublicUrl(Method method,String url) {
		PublicMethod pubAnno = method.getDeclaringClass().getAnnotation(PublicMethod.class);
        if (pubAnno == null) {
        	pubAnno = method.getAnnotation(PublicMethod.class);
        }
        if (pubAnno != null) {
        	piscesPermissionService.addPublicUrl(url);
        }
	}
}
